package org.ktx.osr.audio

import android.media.AudioFormat
import android.media.AudioAttributes
import android.media.projection.MediaProjection
import org.ktx.osr.ui.AppSettings

/**
 * 音频录制配置类
 * 封装音频录制的所有配置参数
 */
data class AudioRecordingConfig(
    val sampleRate: Int = 44100,
    val channelConfig: Int = AudioFormat.CHANNEL_IN_STEREO,
    val audioFormat: Int = AudioFormat.ENCODING_PCM_16BIT,
    val bufferSizeFactor: Int = 2,
    val useAacEncoding: Boolean = true,
    val includeSystemAudio: Boolean = true,
    val mediaProjection: MediaProjection? = null
) {
    companion object {
        /**
         * 从应用设置创建音频录制配置
         */
        fun fromSettings(settings: AppSettings, mediaProjection: MediaProjection?): AudioRecordingConfig {
            return AudioRecordingConfig(
                useAacEncoding = true, // 默认使用AAC编码
                includeSystemAudio = settings.includeSystemSound,
                mediaProjection = mediaProjection
            )
        }
    }
    
    /**
     * 计算缓冲区大小
     */
    fun calculateBufferSize(): Int {
        return android.media.AudioRecord.getMinBufferSize(
            sampleRate,
            channelConfig,
            audioFormat
        ) * bufferSizeFactor
    }
    
    /**
     * 创建音频属性配置（用于系统音频捕获）
     */
    fun createAudioAttributes(): AudioAttributes {
        return AudioAttributes.Builder()
            .setUsage(AudioAttributes.USAGE_MEDIA)
            .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
            .build()
    }
}
